package org.cll.pattern.singleton.exampleone;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @ClassName Logger
 * @Description TODO
 * @Author Administrator
 * @Date 2024-09-04 18:13:52
 * @Version 1.0
 **/
public class Logger {
    private FileWriter writer;

    public Logger() throws IOException {
        File file = new File("D:\\study\\design-patterns-beauty\\log.txt");
        writer = new FileWriter(file, true); // true 表示追加写入
    }

//    private static final Logger instance;
//
//    static {
//        try {
//            instance = new Logger();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public static Logger getInstance() {
//        return instance;
//    }


    public void log(String message) throws IOException, InterruptedException {
//        synchronized(this){
            // 写入正确的 message
            writer.write(message+'\n');
            System.out.println(message);
            // 添加换行符
//            writer.write(System.lineSeparator());
//        // 刷新缓冲区
            writer.flush();
//        }


    }

    // 添加一个关闭方法
    public void close() throws IOException {
        if (writer != null) {
            writer.close();
        }
    }

//    public static void main(String[] args) {
////        try {
////            Logger logger = new Logger();
////            logger.log("This is a test message.");
////            logger.close();
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//
//        for (int i = 0; i < 10; i++) {
//            if((i&1)==0){
//                    System.out.println("偶数"+i);
//
//            }else {
//                    System.out.println("奇数"+i);
//            }
//
//        }
//    }
public static void main(String[] args) {
    try {
        Logger logger1 = new Logger();
        Logger logger2 = new Logger();
        Logger logger3 = new Logger();
        Logger logger4 = new Logger();
        Logger logger5 = new Logger();


        Thread thread1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    logger1.log("线程1的日志消息 " + i);
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    logger2.log("线程2的日志消息 " + i);
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    logger3.log("线程3的日志消息 " + i);
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread4 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    logger3.log("线程4的日志消息 " + i);
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread5 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    logger1.log("线程5的日志消息 " + i);
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();



        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        thread5.join();

        System.out.println("所有线程执行完毕。");
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
}

